class Post < ActiveRecord::Base
  belongs_to :author,   :class_name => 'User'
  has_many   :comments, :dependent  => :destroy

  named_scope :published,   :conditions => { :is_published => true }
  named_scope :draft,       :conditions => { :is_published => false } do
    def publish
      each { |post| post.update_attribute(:is_published, true) }
    end
  end

  named_scope :has_comment, :conditions => [ 'comments_count > 0' ]

  named_scope :recent,    lambda { |*date| {:conditions => { :created_at => date.first || 1.weeks.ago } } }
  named_scope :before,    lambda{ |date| { :conditions => ['created_at > ?',  date] } }

  validates_presence_of :title
  validates_presence_of :body
end

#Post.published.before(1.days.ago)
#Post.recent.publish

## Store named scopes
#published = Post.scoped(:conditions => { :is_published => true })
#recent    = Post.scoped(:conditions => { :created_at => 1.weeks.ago })
#
## Which can be combined
#recent_published = recent.published